/*
 * LogEx a Java library for extracting log file data
 * Copyright 2012 LogEx Team.
 *
 * This program is free software. You can distribute or modify it under the 
 * terms of the GNU Lesser General Public License version 3.0 as published by  
 * the Free Software Foundation.
 *
 * This program is distributed AS IS and WITHOUT WARRANTY. OF ANY KIND,
 * INCLUDING MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, contact the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
package org.logex.main;

import java.io.*;

/**
 * Performs the task of identifying the last read line of a particular log file.
 * 
 * @author Rajith Chathunga
 */
public class LineDetector {

	private String fileName;
	private String userHome;
	private int counter;
	private String[] temp;
	private File file;
	private static final String LOGEX_FOLDER_NAME = "logex";
	private static final String STORAGE_FILE_NAME = "lastreadline.txt";
	
	/**
	 * Creates a folder in user home to save files.
	 * 
	 * @throws IOException
	 *             when failed or interrupted I/O operations
	 */
	private void makeDir() throws IOException {
		File f = new File(userHome + File.separator + LOGEX_FOLDER_NAME);
		f.mkdir();
		if (!f.exists()) {
			f.createNewFile();
		}
	}

	/**
	 * Gets the user home URL.
	 * 
	 * @return the user home URL.
	 * 
	 */
	private String getUserHome() {
		return System.getProperty(StringConstants.USER_HOME);
	}

	/**
	 * Writes the line number that is last read from the log file to the system
	 * file.
	 * 
	 * @param currentLine
	 *            The line number that is last read from the log file
	 * @param logFile
	 *            Name of the log file
	 * @throws IOException
	 *             when failed or interrupted I/O operations
	 */
	public void writeToFile(int currentLine, String logFile) throws IOException {
		FileWriter fstream = new FileWriter(fileName, true);
		BufferedWriter writer = new BufferedWriter(fstream);
		PrintWriter out = new PrintWriter(writer);
		out.println(logFile + "  :  " + currentLine);
		out.close();
	}

	/**
	 * Reads the system file to find the number of lines in the previous log
	 * file.
	 * 
	 * @param logFile
	 *            Name of the log file
	 * @throws IOException
	 *             when failed or interrupted I/O operations
	 * @throws NumberFormatException
	 *             occurs when attempted to convert a string to one of the
	 *             numeric types
	 */
	public int checkFile(String logFile) throws NumberFormatException,
			IOException {
		userHome = getUserHome();
		fileName = userHome + File.separator + LOGEX_FOLDER_NAME
				+ File.separator + STORAGE_FILE_NAME;
		String checker;
		boolean status = false;
		file = new File(fileName);
		if (!file.exists()) {
			makeDir();
			file.createNewFile();
		}
		FileInputStream fstream = new FileInputStream(fileName);
		DataInputStream in = new DataInputStream(fstream);
		BufferedReader br = new BufferedReader(new InputStreamReader(in));
		while ((checker = br.readLine()) != null) {
			temp = checker.split("  :  ");
			if (temp[0].equals(logFile)) {
				counter = Integer.parseInt(temp[1]);
				status = true;
			}
		}
		if (!status) {
			counter = 0;
		}
		return counter;
	}
}
